package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.mall.dto.ClearCartItemRequest;
import com.cskaoyan.mall.dto.ClearCartItemResponse;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.service.ICartService;
import org.redisson.Redisson;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;

/**
 * @auther cskaoyan
 * @date 2022/6/16:16:34
 */
@Service
public class ICartSeviceImpl implements ICartService {

    @Autowired
    RedissonClient redissonClient;

    @Autowired
    ItemMapper itemMapper;

    @Override
    public CartListByIdResponse getCartListById(CartListByIdRequest request) {
        CartListByIdResponse cartListByIdResponse = new CartListByIdResponse();
        try {
            request.requestCheck();
            Long uid = request.getUserId();

            Map<String, CartProductDto> map = redissonClient.getMap(uid.toString());
            Set<Map.Entry<String, CartProductDto>> entries = map.entrySet();
            ArrayList<CartProductDto> cartProductDtos = new ArrayList<>();
            for (Map.Entry<String, CartProductDto> entry : entries) {
                CartProductDto cartProductDto = entry.getValue();
                cartProductDtos.add(cartProductDto);
            }

            cartListByIdResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            cartListByIdResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            cartListByIdResponse.setCartProductDtos(cartProductDtos);

            System.out.println(cartListByIdResponse);

        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(cartListByIdResponse, e);
        }
        return cartListByIdResponse;
    }

    @Override
    public AddCartResponse addToCart(AddCartRequest request) {
        AddCartResponse addCartResponse = new AddCartResponse();
        try {
            request.requestCheck();
            Long uid = request.getUserId();
            Long pid = request.getItemId();
            Integer num = request.getNum();

            Map<String, CartProductDto> map = redissonClient.getMap(uid.toString());
            CartProductDto cartProductDto = new CartProductDto();

            Item item = itemMapper.selectByPrimaryKey(pid);

            cartProductDto.setProductId(pid);
            cartProductDto.setChecked("true");
            cartProductDto.setProductNum((long) num);
            cartProductDto.setLimitNum((long) item.getLimitNum());
            cartProductDto.setSalePrice(item.getPrice());
            cartProductDto.setProductName(item.getTitle());
            cartProductDto.setProductImg(item.getImageBig());

            map.put(pid.toString(), cartProductDto);

            addCartResponse.setResult("成功");
            addCartResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            addCartResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());

        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(addCartResponse, e);
        }
        return addCartResponse;
    }

    @Override
    public UpdateCartNumResponse updateCartNum(UpdateCartNumRequest request) {
        UpdateCartNumResponse updateCartNumResponse = new UpdateCartNumResponse();
        try {
            request.requestCheck();
            Long uid = request.getUserId();
            Long pid = request.getItemId();
            Integer num = request.getNum();
            String checked = request.getChecked();

            Map<String, CartProductDto> map = redissonClient.getMap(uid.toString());
            Set<Map.Entry<String, CartProductDto>> entries = map.entrySet();

            for (Map.Entry<String, CartProductDto> entry : entries) {
                if (entry.getKey().equals(pid.toString())) {
                    CartProductDto cartProductDto = entry.getValue();
                    cartProductDto.setProductNum((long) num);
                    cartProductDto.setChecked(checked);
                    map.put(pid.toString(), cartProductDto);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(updateCartNumResponse, e);
        }

        updateCartNumResponse.setResult("成功");
        updateCartNumResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
        updateCartNumResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        return updateCartNumResponse;
    }

    @Override
    public CheckAllItemResponse checkAllCartItem(CheckAllItemRequest request) {
        CheckAllItemResponse checkAllItemResponse = new CheckAllItemResponse();
        try {
            request.requestCheck();
            Long uid = request.getUserId();
            String checked = request.getChecked();
            Map<String, CartProductDto> map = redissonClient.getMap(uid.toString());
            Set<Map.Entry<String, CartProductDto>> entries = map.entrySet();
            for (Map.Entry<String, CartProductDto> entry : entries) {
                CartProductDto cartProductDto = entry.getValue();
                cartProductDto.setChecked(checked);
                map.put(cartProductDto.getProductId().toString(), cartProductDto);
            }
            checkAllItemResponse.setResult("成功");
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(checkAllItemResponse, e);
        }
        checkAllItemResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
        checkAllItemResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        return checkAllItemResponse;
    }

    @Override
    public DeleteCartItemResponse deleteCartItem(DeleteCartItemRequest request) {
        DeleteCartItemResponse deleteCartItemResponse = new DeleteCartItemResponse();
        try {
            request.requestCheck();
            Long uid = request.getUserId();
            Long pid = request.getItemId();
            Map<String, CartProductDto> map = redissonClient.getMap(uid.toString());
            map.remove(pid.toString());
            deleteCartItemResponse.setResult("成功");
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(deleteCartItemResponse, e);
        }
        deleteCartItemResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
        deleteCartItemResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        return deleteCartItemResponse;
    }

    @Override
    public DeleteCheckedItemResposne deleteCheckedItem(DeleteCheckedItemRequest request) {
        DeleteCheckedItemResposne deleteCheckedItemResposne = new DeleteCheckedItemResposne();
        try {
            request.requestCheck();
            Long uid = request.getUserId();
            Map<String, CartProductDto> map = redissonClient.getMap(uid.toString());
            Set<Map.Entry<String, CartProductDto>> entries = map.entrySet();
            for (Map.Entry<String, CartProductDto> entry : entries) {
                if (entry.getValue().getChecked().equals("true")) {
                    map.remove(entry.getKey());
                }
            }
            deleteCheckedItemResposne.setResult("成功");
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(deleteCheckedItemResposne, e);
        }
        deleteCheckedItemResposne.setCode(ShoppingRetCode.SUCCESS.getCode());
        deleteCheckedItemResposne.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        return deleteCheckedItemResposne;
    }

    @Override
    public ClearCartItemResponse clearCartItemByUserID(ClearCartItemRequest request) {
        ClearCartItemResponse clearCartItemResponse = new ClearCartItemResponse();
        try {
            request.requestCheck();
            Long uid = request.getUserId();
            Map<String, CartProductDto> map = redissonClient.getMap(uid.toString());
            Set<Map.Entry<String, CartProductDto>> entries = map.entrySet();
            for (Map.Entry<String, CartProductDto> entry : entries) {
                if (entry.getValue().getChecked().equals("true")) {
                    map.remove(entry.getKey());
                }
            }
            clearCartItemResponse.setResult("成功");
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(clearCartItemResponse, e);
        }
        clearCartItemResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
        clearCartItemResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        return clearCartItemResponse;
    }
}
